package com.example.jikeyoujikeyou.office_sz.view.activity.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.jikeyoujikeyou.office_sz.R;
import com.example.jikeyoujikeyou.office_sz.view.activity.register.VCompanyFragment;
import com.example.jikeyoujikeyou.office_sz.view.base.BaseActivity;
import com.example.jikeyoujikeyou.office_sz.view.base.BaseFragment;
import com.example.jikeyoujikeyou.office_sz.view.base.OfficePageAdapter;
import com.example.jikeyoujikeyou.office_sz.view.base.OfficeViewPager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements BaseFragment.OnFragmentSenderMessage  {

    private View nav_chat;
    private View nav_contract;
    private View nav_workspace;
    private View nav_my;

    private TextView tv_chat,tv_contract,tv_workspace,tv_my;

    private String[] title = {
            "企聊","通讯录","工作台","我的"
    };

    private OfficeViewPager ovp;
    RadioGroup rg;
    List<BaseFragment> fragments;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ovp = (OfficeViewPager) findViewById(R.id.ovp);
        rg = (RadioGroup) findViewById(R.id.rg);

        nav_chat = findViewById(R.id.nav_chat);
        nav_contract = findViewById(R.id.nav_contract);
        nav_workspace = findViewById(R.id.nav_workspace);
        nav_my = findViewById(R.id.nav_my);

        tv_chat = (TextView) findViewById(R.id.tv_chat);
        tv_contract = (TextView) findViewById(R.id.tv_contract);
        tv_workspace = (TextView) findViewById(R.id.tv_workspace);
        tv_my = (TextView) findViewById(R.id.tv_my);

        fragments = new ArrayList<>();
        fragments.add(new VChatFragment().setOnFragmentMessage(this));
        fragments.add(new VContractFragment().setOnFragmentMessage(this));
        fragments.add(new VCompanyFragment().setOnFragmentMessage(this));
        fragments.add(new VMyFragment().setOnFragmentMessage(this));

        getTitleBar().getIv_return().setVisibility(View.INVISIBLE);

        ovp.setMove(false);
        ovp.setAdapter(new OfficePageAdapter(getSupportFragmentManager(), fragments));
        ovp.setCurrentItem(0);
        rg.getChildAt(0).setSelected(true);
        setTitle("企聊");
        addListener();
    }

    private void addListener(){
        for (int i = 0; i < 4; i++) {
            final int finalI = i;
            rg.getChildAt(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    changed(finalI,title[finalI]);
                }
            });
        }

        ovp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                changed(position,title[position]);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void changed(int currentItem,String title){
        System.out.println("currentItem:"+currentItem);
        System.out.println("title:"+title);
        ovp.setCurrentItem(currentItem);
        rg.getChildAt(0).setSelected(false);
        rg.getChildAt(1).setSelected(false);
        rg.getChildAt(2).setSelected(false);
        rg.getChildAt(3).setSelected(false);
        rg.getChildAt(currentItem).setSelected(true);
        tv_chat.setSelected(currentItem == 0);
        tv_contract.setSelected(currentItem == 1);
        tv_workspace.setSelected(currentItem == 2);
        tv_my.setSelected(currentItem == 3);
        setTitle(title);
    }

    @Override
    public <T> void message(BaseFragment fragment, T t) { }

    private void setTitle(String title){
        getTitleBar().getTv_title().setText(title);
    }

}
